import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

//= =======引入子路由=========
import targetSystem from './targetSystem'; // 指标体系
import systemRouter from './system'
import improveRouter from './domsImprove'
import reportRouter from './report' // 诊改报告

const Layout = () => import('@/components/layout');
const Index = () => import('@/views/index');
const login = () => import('@/views/login');
const Cockpit = () => import('@/views/cockpit/cockpit.vue')

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
};

Vue.use(VueRouter);

NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: false, // 是否显示加载ico
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
})

// auth: true/false 判断是否需要登录权限
const routes = [
    {
        path: '/',
        component: Layout,
        auth: false,
        id: ''
    },
    {
        path: '/login',
        component: login,
        name: 'login',
        meta: {
            title: '登录',
            auth: false,
            id: ''
        }
    },
    {
        path: '/index',
        component: Layout,
        children: [
            {
                path: '/index',
                component: Index,
                name: 'index',
                meta: {
                    title: '首页',
                    auth: true,
                    id: ''
                }
            },
            targetSystem,
            systemRouter,
            improveRouter,
            reportRouter
        ]
    },
    {
        path: '/portrait',
        name: 'portrait',
        meta: {
            title: '诊改画像',
            auth: false,
            id: ''
        },
        component: Layout,
        children: [
            {
                path: '/portrait:type?',
                name: 'portrait',
                meta: {
                    title: '诊改状态大屏',
                    auth: false,
                    id: ''
                },
                component: () => import('@/views/portrait/screen')
            }
        ]
    },
    {
        path: '/cockpit',
        component: Layout,
        children: [
            {
                path: '/cockpit',
                component: Cockpit,
                name: '诊改驾驶舱',
                meta: {
                    title: '',
                    auth: true,
                    id: ''
                }
            }
        ]
    },
    {
        path: '*',
        name: '404',
        // component: () => import('@/views/error')
        component: Layout,
        children: [
            {
                path: '*',
                name: '404',
                meta: {
                    title: '404',
                    auth: false,
                    id: '',
                    isError: true
                },
                component: () => import('@/views/error')
            }
        ]
    }
];

const router = new VueRouter({
    mode: 'hash',
    routes
});

router.beforeEach((to, from, next) => {
    // 每次切换页面时，调用进度条
    NProgress.start();

    switch (to.params.type) {
        case 'XT_WH_XX':
            to.meta.title = '学校层';
            break;
        case 'XT_WH_ZY':
            to.meta.title = '专业层';
            break;
        case 'XT_WH_KC':
            to.meta.title = '课程层';
            break;
        case 'XT_WH_JS':
            to.meta.title = '教师层';
            break;
        case 'XT_WH_XS':
            to.meta.title = '学生层';
            break;
    }

    // if (to.meta.auth) {
    //     if (to.name != 'login' && !sessionStorage.getItem('userInfo')) {    // 没登录则跳转到登录界面
    //         next({
    //             path: '/login',
    //             query: {redirect: to.fullPath}
    //         })
    //     } else {
    //         next()
    //     }
    // } else {
    //     next();
    // }
    next();
});

router.afterEach(to => {
    // 在即将进入新的页面组件前，关闭掉进度条
    NProgress.done()
})

export default router
